﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for PhieuNhap
/// </summary>
public class PhieuNhapCtr
{
	public PhieuNhapCtr()
	{
		
	}
    
    public EPhieuNhap layPhieuNhap(int MaPN)
    {
        try
        {
            DataTable tablePhieuNhap = DataProvider.GetData("Select * from PhieuNhap Where MaPN=@MaPN", "@MaPN", MaPN);
            EPhieuNhap phieuNhap = new EPhieuNhap();
            phieuNhap.MaPN = Int32.Parse(tablePhieuNhap.Rows[0]["MaPN"].ToString());
            phieuNhap.NgayNhap = DateTime.Parse(tablePhieuNhap.Rows[0]["NgayNhap"].ToString());
            phieuNhap.MaNCC = Int32.Parse(tablePhieuNhap.Rows[0]["MaNCC"].ToString());
            phieuNhap.MaNV = Int32.Parse(tablePhieuNhap.Rows[0]["MaNV"].ToString());

            return phieuNhap;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public EChiTietPhieuNhap layChiTietPhieuNhap(int MaPN)
    {
        try
        {
            DataTable tableChiTietPhieuNhap = DataProvider.GetData("Select * from ChiTietPhieuNhap Where MaPN=@MaPN", "@MaPN", MaPN);
            EChiTietPhieuNhap chiTietPhieuNhap = new EChiTietPhieuNhap();
            chiTietPhieuNhap.MaPN = Int32.Parse(tableChiTietPhieuNhap.Rows[0]["MaPN"].ToString());
            chiTietPhieuNhap.MaSP = tableChiTietPhieuNhap.Rows[0]["MaSP"].ToString();
            chiTietPhieuNhap.SoLuong = Int32.Parse(tableChiTietPhieuNhap.Rows[0]["SoLuong"].ToString());
            chiTietPhieuNhap.Gia = tableChiTietPhieuNhap.Rows[0]["Gia"].ToString();

            return chiTietPhieuNhap;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool DeleteChiTietPhieuNhap(int MaPN)
    {
        try
        {
            DataProvider.ExecuteNonQuery("DELETE FROM ChiTietPhieuNhap WHERE MaPN=@MaPN", "@MaPN", MaPN);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool DeletePhieuNhap(int MaPN)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update PhieuNhap Set Trangthai=0 WHERE MaPN=@MaPN", "@MaPN", MaPN);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool InsertChiTietPhieuNhap(EChiTietPhieuNhap chiTietPN)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO ChiTietPhieuNhap VAlUES(@MaPN,@MaSP,@SoLuong,@Gia)",
                 "@MaPN", chiTietPN.MaPN, "@MaSP", chiTietPN.MaSP, "@SoLuong", chiTietPN.SoLuong, "@Gia", chiTietPN.Gia);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public bool InsertPhieuNhap(EPhieuNhap phieuNhap)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO ChiTietPhieuNhap VAlUES(@MaPN,@NgayNhap,@MaNCC,@MaNV,@TrangThai)",
                "@MaPN", phieuNhap.MaPN, "@NgayNhap", phieuNhap.NgayNhap, "@MaNCC", phieuNhap.MaNCC, "@MaNV", phieuNhap.MaNV, "@TrangThai", phieuNhap.TrangThai);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    
    public bool UpdatePhieuNhap(EPhieuNhap phieuNhap)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update PhieuNhap set NgayNhap=@NgayNhap,MaNCC=@MaNCC,MaNV=@MaNV,TrangThai=@TrangThai WHERE MaPN=@MaPN", "@MaPN", phieuNhap.MaPN, "@NgayNhap", phieuNhap.NgayNhap, "@MaNCC", phieuNhap.MaNCC, "@MaNV", phieuNhap.MaNV, "@TrangThai",phieuNhap.TrangThai);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool UpdateChiTietPhieuNhap(EChiTietPhieuNhap CTPhieuNhap)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update ChiTietPhieuNhap set MaSP=@MaSP,SoLuong=@SoLuong,Gia=@Gia WHERE MaPN=@MaPN", "@MaPN", CTPhieuNhap.MaPN, "@MaSP", CTPhieuNhap.MaSP, "@SoLuong", CTPhieuNhap.SoLuong, "@Gia", CTPhieuNhap.Gia);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    //Hanh
    public DataTable layPhieuNhapH()
    {
        try
        {
            string sql = "Select PhieuNhap.*,TenNhaCC,TenNV from PhieuNhap,NhaCungCap,NhanVien where phieunhap.maNCC=NhaCungCap.maNhaCC and phieunhap.maNV=NhanVien.MaNV and PhieuNhap.TrangThai=1 ";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable layPhieuNhapSua(string maSP)
    {
        try
        {
            string sql = "select PhieuNhap.*,TenNhaCC,TenNV from PhieuNhap,NhaCungCap,NhanVien where phieunhap.maNCC=NhaCungCap.maNhaCC and phieunhap.maNV=NhanVien.MaNV and PhieuNhap.TrangThai=1 and MAPN in (select MaPN from ChiTietPhieuNhap where MaSP=@MaSP)";
            return DataProvider.GetData(sql,"@MaSP",maSP);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable layPhieuNhapSuaTheoMaPN(int maPN)
    {
        try
        {
            string sql = "select PhieuNhap.*,TenNhaCC,TenNV from PhieuNhap,NhaCungCap,NhanVien where phieunhap.maNCC=NhaCungCap.maNhaCC and phieunhap.maNV=NhanVien.MaNV and PhieuNhap.TrangThai=1 and MAPN=@MaPN";
            return DataProvider.GetData(sql, "@MaPN", maPN);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable layMaSPPhieuNhapH()
    {
        try
        {
            string sql = "select * from SanPham where masp not in(select masp from ChitietphieuNhap) and TrangThai=1";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool InsertPhieuNhapH(EPhieuNhap phieuNhap)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO PhieuNhap VAlUES(@NgayNhap,@MaNCC,@MaNV,@TrangThai)", "@NgayNhap", phieuNhap.NgayNhap, "@MaNCC", phieuNhap.MaNCC, "@MaNV", phieuNhap.MaNV, "@TrangThai", phieuNhap.TrangThai);
            return true;
        }
        catch (Exception ex)
        {
            //throw ex;
            return false;
        }
    }
    public EPhieuNhap layPhieuNhapH(int MaPN)
    {
        try
        {
            DataTable tablePhieuNhap = DataProvider.GetData("Select * from PhieuNhap Where MaPN=@MaPN", "@MaPN", MaPN);
            EPhieuNhap phieuNhap = new EPhieuNhap();
            phieuNhap.MaPN = Int32.Parse(tablePhieuNhap.Rows[0]["MaPN"].ToString());
            phieuNhap.NgayNhap = DateTime.Parse(tablePhieuNhap.Rows[0]["NgayNhap"].ToString());
            phieuNhap.MaNCC = Int32.Parse(tablePhieuNhap.Rows[0]["MaNCC"].ToString());
            phieuNhap.MaNV = Int32.Parse(tablePhieuNhap.Rows[0]["MaNV"].ToString());

            return phieuNhap;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable layDSChiTietPhieuNhap(int maPN)
    {
        try
        {
            string sql = "Select a.*,b.TenSP from ChiTietPhieuNhap a,SanPham b where maPN=@MaPN and a.MaSP=b.MaSP ";
            return DataProvider.GetData(sql, "@MaPN", maPN);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool UpdatePhieuNhapH(EPhieuNhap phieuNhap)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update PhieuNhap set NgayNhap=@NgayNhap,MaNCC=@MaNCC,MaNV=@MaNV WHERE MaPN=@MaPN", "@MaPN", phieuNhap.MaPN, "@NgayNhap", phieuNhap.NgayNhap, "@MaNCC", phieuNhap.MaNCC, "@MaNV", phieuNhap.MaNV);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public bool InsertChiTietPhieuNhapH(EChiTietPhieuNhap chiTietPN)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO ChiTietPhieuNhap VAlUES(@MaPN,@MaSP,@SoLuong,@Gia)",
                 "@MaPN", chiTietPN.MaPN, "@MaSP", chiTietPN.MaSP, "@SoLuong", chiTietPN.SoLuong, "@Gia", chiTietPN.Gia);
            return true;
        }
        catch (Exception ex)
        {
            //throw ex;
            return false;
        }
    }
    public bool UpdateChiTietPhieuNhapH(EChiTietPhieuNhap CTPhieuNhap,string maSP)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update ChiTietPhieuNhap set MaSP=@MaSPU,SoLuong=@SoLuong,Gia=@Gia WHERE MaPN=@MaPN and MaSP=@MaSP", "@MaPN", CTPhieuNhap.MaPN, "@MaSPU", CTPhieuNhap.MaSP, "@SoLuong", CTPhieuNhap.SoLuong, "@Gia", CTPhieuNhap.Gia,"@MaSP",maSP);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool DeleteChiTietPhieuNhapH(int MaPN, int maSP)
    {
        try
        {
            DataProvider.ExecuteNonQuery("DELETE FROM ChiTietPhieuNhap WHERE MaPN=@MaPN and MaSP=@MaSP", "@MaPN", MaPN, "@MaSP", maSP);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public EChiTietPhieuNhap layChiTietPhieuNhapTheoSPH(int maPN,string MaSP)
    {
        try
        {
            DataTable tableChiTietPhieuNhap = DataProvider.GetData("Select * from ChiTietPhieuNhap Where MaSP=@MaSP and MaPN=@MaPN","@MaPN",maPN, "@MaSP", MaSP);
            EChiTietPhieuNhap chiTietPhieuNhap = new EChiTietPhieuNhap();
            chiTietPhieuNhap.MaPN = Int32.Parse(tableChiTietPhieuNhap.Rows[0]["MaPN"].ToString());
            chiTietPhieuNhap.MaSP = tableChiTietPhieuNhap.Rows[0]["MaSP"].ToString();
            chiTietPhieuNhap.SoLuong = Int32.Parse(tableChiTietPhieuNhap.Rows[0]["SoLuong"].ToString());
            chiTietPhieuNhap.Gia = tableChiTietPhieuNhap.Rows[0]["Gia"].ToString();

            return chiTietPhieuNhap;
        }
        catch (Exception ex)
        {
            throw ex;
            //return false;
        }
    }
    public DataTable layMaSPPhieuNhap1H(string maSP)
    {
        try
        {
            string sql = "select * from SanPham where (masp not in(select masp from ChitietphieuNhap) or masp=@maSP) and TrangThai=1";
            return DataProvider.GetData(sql, "@maSP", maSP);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable kiemTraMaSP(string maSP)
    {
        try
        {
            string sql = "select * from SanPham where masp=@maSP and TrangThai=1";
            return DataProvider.GetData(sql, "@maSP", maSP);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

}